<?php
require_once 'Zend/Db.php';
require_once 'Zend/Config/Ini.php';
//require_once 'FtpHelper.php';
/**
 * 调度控制脚本的守护程序
 * @author Administrator
 *
 */
class minicmd{
	private $minicmdpath="";
	private $minicmdwebpath="";
   function run(){
   		$this->minicmdpath = "/data/web/library/Common/Command/";//命令路径
		$this->minicmdwebpath = "/data/web/public/minicmd/";//命令中使用的web路径
		system("rm -f ".$this->minicmdpath."/signo*");
		if (!file_exists($this->minicmdwebpath)){
    			system("mkdir -p ".$this->minicmdwebpath);}//创建路径
    	if(file_exists($this->minicmdpath."/minicmd.pid")){
    		$oldpid = file_get_contents($this->minicmdpath."/minicmd.pid");
    		system("kill $oldpid");
    	}
   		$pid = posix_getpid();
	    file_put_contents($this->minicmdpath."/minicmd.pid",$pid);
		function sig_handler($signo)
		{   global $stop_flag;
			syslog(LOG_INFO, ": commands - $signo.");//写入日志
    		$minicmdpath = "/data/web/library/Common/Command/";
			$signofile = "$minicmdpath/signo$signo";
		    if(!file_exists($signofile)){
			    system("touch $signofile");//生成文件
			    system("php ".$minicmdpath."miniFun.php ".$signo);
			    system("rm -f $signofile");//删除
		    }    		
		}
		declare(ticks = 1);
		$stop_flag = false;
		// setup signal handlers				
		pcntl_signal(35, "sig_handler");//切换为主控
		pcntl_signal(36, "sig_handler");//重启mysql
		pcntl_signal(37, "sig_handler");//激活受控端
		pcntl_signal(38, "sig_handler");//重载配置文件
		pcntl_signal(39, "sig_handler");
		pcntl_signal(40, "sig_handler");
		pcntl_signal(41, "sig_handler");
		pcntl_signal(42, "sig_handler");
		pcntl_signal(43, "sig_handler");
		pcntl_signal(44, "sig_handler");//通知关闭
		pcntl_signal(45, "sig_handler");
		pcntl_signal(46, "sig_handler");
		pcntl_signal(47, "sig_handler");
		pcntl_signal(48, "sig_handler");
		pcntl_signal(49, "sig_handler");
		pcntl_signal(50, "sig_handler");
		pcntl_signal(51, "sig_handler");
		pcntl_signal(52, "sig_handler");
		pcntl_signal(53, "sig_handler");
		pcntl_signal(54, "sig_handler");
		pcntl_signal(55, "sig_handler");
		pcntl_signal(56, "sig_handler");
		pcntl_signal(57, "sig_handler");
		pcntl_signal(58, "sig_handler");
		pcntl_signal(59, "sig_handler");
		pcntl_signal(60, "sig_handler");
		pcntl_signal(61, "sig_handler");
		pcntl_signal(62, "sig_handler");
		pcntl_signal(63, "sig_handler");//存活检测
		pcntl_signal(64, "sig_handler");//主控受控心跳检查
		while(!$stop_flag) {
		    sleep(20);
		    system("php ".$this->minicmdpath."miniFun.php 64");//每半分钟执行一次
		}
    }
}
$command = new minicmd();
$command->run();
